package com.kaili.tms.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.common.tenant.DbContextHolder;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.tms.mapper.SchedulingPlanMapper;
import com.kaili.tms.mapper.ShiftsRulesMapper;
import com.kaili.tms.pojo.dto.SchedulingPlanDTO;
import com.kaili.tms.pojo.dto.SchedulingPlanDetailsAddDTO;
import com.kaili.tms.pojo.dto.SchedulingPlanDetailsUpdateDTO;
import com.kaili.tms.pojo.dto.SchedulingPlanUpdateDTO;
import com.kaili.tms.pojo.entity.*;
import com.kaili.tms.pojo.vo.*;
import com.kaili.tms.service.SchedulingPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @version 1.0
 * @author: xinglei @Time: 19-2-14 上午9:10
 * @Copyright: © 2018 杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅,
 * 禁止外泄或用于其它商业目的
 */
@Service
public class SchedulingPlanServiceImpl implements SchedulingPlanService {
    @Autowired
    private SchedulingPlanMapper schedulingPlanMapper;
    @Autowired
    private ShiftsRulesMapper shiftsRulesMapper;

    @Override
    public String getVehicleCode(Integer sitId) {
        return schedulingPlanMapper.getVehicleCode(sitId);
    }

    @Override
    public Page<SchedulingPlanVO> queryschedulingPlan(Page<SchedulingPlanVO> page, SchedulingPlanDTO schedulingPlanDTO) {

        List<SchedulingPlanVO> list = schedulingPlanMapper.queryschedulingPlan(page, schedulingPlanDTO);
        return page.setRecords(list);
    }

    @Override
    public Integer schedulingPlanAdd(SchedulingPlanAddEnity schedulingPlanAdd) {
        List<SchedulingPlanVO> mlist = schedulingPlanMapper.getSchedulingPlanId(schedulingPlanAdd);
        if (mlist != null && mlist.size() > 0) {
            return 1;
        }
        schedulingPlanAdd.setSiteId(LoginDetailUtils.getSiteId());
        schedulingPlanMapper.schedulingPlanAdd(schedulingPlanAdd);
        return 2;
    }

    @Override
    public void schedulingPlanUpdate(SchedulingPlanUpdateDTO schedulingPlan0UpdateDTO) {
        SchedulingPlanUpdateEntity schedulingPlanUpdateEntity = new SchedulingPlanUpdateEntity();
        BeanUtils.copyProperties(schedulingPlan0UpdateDTO, schedulingPlanUpdateEntity);
        schedulingPlanUpdateEntity.setModifyTime(new Date());
        schedulingPlanUpdateEntity.setModifyBy(LoginDetailUtils.getCurrentEmployeeId());
        schedulingPlanMapper.schedulingPlanUpdate(schedulingPlanUpdateEntity);
    }

    @Override
    public List<SchedulingDetailShiftsRulesVO> getAllShiftsRules(Long carLineId) {
        return schedulingPlanMapper.getAllShiftsRules(carLineId);
    }

    @Override
    public Integer shiftsSchedulingAdd(SchedulingPlanDetailsAddDTO schedulingPlanDetailsAddDTO) {
        try {

            if (schedulingPlanDetailsAddDTO.getFlag() == 1) {
                //插入计划班次关联表
                for (int i = 0; i < schedulingPlanDetailsAddDTO.getShiftsSchedulingEntities().size(); i++) {
                    ShiftsSchedulingEntity shiftsSchedulingEntity = schedulingPlanDetailsAddDTO.getShiftsSchedulingEntities().get(i);
                    shiftsSchedulingEntity.setCreatedTime(new Date());
                    shiftsSchedulingEntity.setCreatedBy(LoginDetailUtils.getCurrentEmployeeId());
                    shiftsSchedulingEntity.setSiteId(LoginDetailUtils.getSiteId());
                    schedulingPlanMapper.shiftsSchedulingAdd(shiftsSchedulingEntity);
                }
                //插入计化明细表
                for (int i = 0; i < schedulingPlanDetailsAddDTO.getSchedulingPlanDetailsAddEntities().size(); i++) {
                    String carCode = vehicleCode();
                    SchedulingPlanDetailsAddEntity schedulingPlanDetailsAddEntity = schedulingPlanDetailsAddDTO.getSchedulingPlanDetailsAddEntities().get(i);
                    schedulingPlanDetailsAddEntity.setSiteId(LoginDetailUtils.getSiteId());
                    //车次编码自动生成
                    schedulingPlanDetailsAddEntity.setCarTrainNum(carCode);
                    schedulingPlanDetailsAddEntity.setCreatedTime(new Date());
                    schedulingPlanDetailsAddEntity.setCreatedBy(LoginDetailUtils.getCurrentEmployeeId());
                    schedulingPlanMapper.schedulingPlanDetailAdd(schedulingPlanDetailsAddEntity);
                }
                //将排班状态改为已经排班
                SchedulingPlanDetailsAddEntity schedulingPlanDetailsAddEntity = schedulingPlanDetailsAddDTO.getSchedulingPlanDetailsAddEntities().get(1);
                schedulingPlanMapper.updateArlary(schedulingPlanDetailsAddEntity.getPlanId().longValue());
            } else {
                //修改
                /*for (int i = 0; i < schedulingPlanDetailsAddDTO.getShiftsSchedulingEntities().size(); i++) {
                    SchedulingPlanDetailsUpdateDTO schedulingPlanDetailsUpdateDTO1 =  schedulingPlanDetailsAddDTO.getShiftsSchedulingEntities().get(i);
                    SchedulingPlanDetailsUpdateEntity planDetailsUpdateEntity = new SchedulingPlanDetailsUpdateEntity();
                    BeanUtils.copyProperties(schedulingPlanDetailsUpdateDTO1, planDetailsUpdateEntity);
                    planDetailsUpdateEntity.setModifyBy(LoginDetailUtils.getCurrentEmployeeId());
                    planDetailsUpdateEntity.setModifyTime(new Date());
                    schedulingPlanMapper.schedulingPlanDetailUpdate(planDetailsUpdateEntity);
                } */
            }
        } catch (Exception e) {
            return 1;
        }
        return 0;
    }

    @Override
    public Integer schedulingPlanDelete(Long planId) {
        //根据排版计划ID查询排版计划明细
        List<SchedulingPlanDetailsVO> mlist = schedulingPlanMapper.getAllschedulingPlanDetail(planId);
        //判断当前班次计划明细是否已经发车 已经发车不能删除
        //mlist为空就证明没有明细可以直接进行删除.
        if (mlist != null && mlist.size() > 0) {
            for (int i = 0; i < mlist.size(); i++) {
                SchedulingPlanDetailsVO schedulingPlanDetailsVO = mlist.get(i);
                Date date = new Date();
                //用发车时间
                if (!date.before(schedulingPlanDetailsVO.getCreatedTime())) {
                    return 1;
                }
            }
            //上面验证通过就可以对明细进行删除了
            for (int i = 0; i < mlist.size(); i++) {
                SchedulingPlanDetailsVO schedulingPlanDetailsVO = mlist.get(i);
                schedulingPlanMapper.schedulingPlanDetailDelete(schedulingPlanDetailsVO.getId());
            }
        }
        //删除明细后排版计划也要进行删除
        schedulingPlanMapper.schedulingPlanDelete(planId);
        return 2;
    }

    //获取车次编码
    public String vehicleCode() {
        System.out.println(LoginDetailUtils.getSiteCode());
        System.out.println(LoginDetailUtils.getSiteCode());
        System.out.println(LoginDetailUtils.getSiteCode());
        String currentDbType = DbContextHolder.getDbType();
        System.out.println(currentDbType);
        //查询已存在最大的编号
        String vehicleCode = schedulingPlanMapper.getVehicleCode(LoginDetailUtils.getSiteId());
        //将str转成integer
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMdd");
        String date1 = simpleDateFormat.format(date);
        vehicleCode = LoginDetailUtils.getSiteCode() + date1 + vehicleCode;
        return vehicleCode;
    }

    @Override
    public String schedulingPlanCode(Integer sitId) {
        Long code = shiftsRulesMapper.getTrainNum();
        return "PB" + LoginDetailUtils.getSiteCode() + code;
    }

    @Override
    public void schedulingPlanDetailUpdate(List<SchedulingPlanDetailsUpdateDTO> schedulingPlanDetailsUpdateDTO) {

        for (int i = 0; i < schedulingPlanDetailsUpdateDTO.size(); i++) {
            SchedulingPlanDetailsUpdateDTO schedulingPlanDetailsUpdateDTO1 = schedulingPlanDetailsUpdateDTO.get(i);
            SchedulingPlanDetailsUpdateEntity planDetailsUpdateEntity = new SchedulingPlanDetailsUpdateEntity();
            BeanUtils.copyProperties(schedulingPlanDetailsUpdateDTO1, planDetailsUpdateEntity);
            planDetailsUpdateEntity.setModifyBy(LoginDetailUtils.getCurrentEmployeeId());
            planDetailsUpdateEntity.setModifyTime(new Date());
            schedulingPlanMapper.schedulingPlanDetailUpdate(planDetailsUpdateEntity);
        }
    }

    @Override
    public List<BaseDriverVO> schedulingPlanCarId(HashMap map) {
        return schedulingPlanMapper.schedulingPlanCarId(map);
    }

    @Override
    public List<DateVO> getDateAndschedulingPlan(String startTime, String endTime, Long carLineId, Long planId) {
        //判断车线是否维护车次
        List<SchedulingDetailShiftsRulesVO> mlist1 = schedulingPlanMapper.getAllShiftsRules(carLineId);


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dBegin = null;
        Map<String, List<DateVO>> map = new HashMap<String, List<DateVO>>();
        List<DateVO> datas = new ArrayList<DateVO>();
        try {
            dBegin = sdf.parse(startTime);
            Date dEnd = sdf.parse(endTime);
            //将时间段进行分割为某个日期
            datas = findDates(dBegin, dEnd);
            if (datas != null && datas.size() > 0) {
                for (int i = 0; i < datas.size(); i++) {
                    DateVO dateVO = datas.get(i);
                    //数据为空的时候为新增 不为空的时候则时修改
                    List<SchedulingPlanDetailsVO> mlist = schedulingPlanMapper.getAllschedulingPlanDetail(planId);
                    //修改分割数据
                    if (mlist != null && mlist.size() > 0) {
                        //所有明细
                        List<SchedulingDetailShiftsRulesVO> shiftsRulesVOS = schedulingPlanMapper.getSchedulingPlanDetal(planId);
                        for (int a = 0; a < shiftsRulesVOS.size(); a++) {
                            SchedulingDetailShiftsRulesVO schedulingDetailShiftsRulesVO = shiftsRulesVOS.get(a);
                            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
                            String date1 = sd.format(schedulingDetailShiftsRulesVO.getStartTime1());
                            //
                            if (dateVO.getDate().equals(date1)) {
                                dateVO.getShiftsRules().add(schedulingDetailShiftsRulesVO);
                            }
                        }
                        if (dateVO.getShiftsRules().size() <= 0) {
                            List<SchedulingDetailShiftsRulesVO> SchedulingDetailShiftsRulesVO = schedulingPlanMapper.getSchedulingPlanDetal1(planId);
                            dateVO.setShiftsRules(SchedulingDetailShiftsRulesVO);
                        }
                    } else {
                        //新增分割数据(新增的时候用)
                        List<SchedulingDetailShiftsRulesVO> shiftsRulesVOS = schedulingPlanMapper.getAllShiftsRules(carLineId);
                        dateVO.setShiftsRules(shiftsRulesVOS);
                    }
                    System.out.println(dateVO);
                }
                return datas;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }


        return datas;
    }

    public List<DateVO> findDates(Date dBegin, Date dEnd) {
        List<DateVO> lDate = new ArrayList<DateVO>();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        DateVO dateVO = new DateVO();
        dateVO.setDate(sd.format(dBegin));
        dateVO.setShiftsRules(new ArrayList<SchedulingDetailShiftsRulesVO>());
        lDate.add(dateVO);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        DateVO dateVO1;//用来接收新的日期对象
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            dateVO1 = new DateVO();
            dateVO1.setDate(sd.format(calBegin.getTime()));
            dateVO1.setShiftsRules(new ArrayList<SchedulingDetailShiftsRulesVO>());
            lDate.add(dateVO1);
        }
        return lDate;
    }

    @Override
    public List<DateVO> getDateAndschedulingPlanRanom(
            String startTime, String endTime, Long carLineId, Long planId) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dBegin = null;
        Map<String, List<DateVO>> map = new HashMap<String, List<DateVO>>();
        List<DateVO> datas = new ArrayList<DateVO>();
        try {
            dBegin = sdf.parse(startTime);
            Date dEnd = sdf.parse(endTime);
            // 将时间段进行分割为某个日期
            datas = findDates(dBegin, dEnd);
            if (datas != null && datas.size() > 0) {
                for (int i = 0; i < datas.size(); i++) {
                    DateVO dateVO = datas.get(i);
                    // 数据为空的时候为新增 不为空的时候则时修改
                    List<SchedulingPlanDetailsVO> mlist =
                            schedulingPlanMapper.getAllschedulingPlanDetail(planId);
                    // 修改分割数据
                    if (mlist != null && mlist.size() > 0) {
                        List<BaseDriverVO> basedrivervlist = schedulingPlanMapper.getCarLinId(carLineId);
                        //List<SchedulingDetailShiftsRulesVO> SchedulingDetailShiftsRulesVOList = schedulingPlanMapper.getSchedulingPlanDetal1(planId);
                        List<SchedulingDetailShiftsRulesVO> shiftsRulesVOS = schedulingPlanMapper.getSchedulingPlanDetal(planId);
                        int m = 0;
                        for (int a = 0; a < shiftsRulesVOS.size(); a++) {
                            SchedulingDetailShiftsRulesVO schedulingDetailShiftsRulesVO = shiftsRulesVOS.get(a);
                            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
                            String date1 = sd.format(schedulingDetailShiftsRulesVO.getStartTime1());
                            if (dateVO.getDate().equals(date1)) {
                                if (m == basedrivervlist.size()) {
                                    m = 0;
                                }
                                BaseDriverVO baseDriverVO = basedrivervlist.get(m++);
                                schedulingDetailShiftsRulesVO.setCarId(baseDriverVO.getCarId());
                                schedulingDetailShiftsRulesVO.setDriverName(baseDriverVO.getDriverName());
                                schedulingDetailShiftsRulesVO.setDriverId(baseDriverVO.getDriverId().toString());
                                schedulingDetailShiftsRulesVO.setCarId(baseDriverVO.getCarId());
                                schedulingDetailShiftsRulesVO.setCarType(baseDriverVO.getCarType());
                                schedulingDetailShiftsRulesVO.setCarNo(baseDriverVO.getCarNumber());
                                dateVO.getShiftsRules().add(schedulingDetailShiftsRulesVO);
                            }
                        }
                   /*   int m = 0;
                        for (int a = 0; a < SchedulingDetailShiftsRulesVOList.size(); a++) {
                            SchedulingDetailShiftsRulesVO schedulingDetailShiftsRulesVO = SchedulingDetailShiftsRulesVOList.get(a);
                            if (m == basedrivervlist.size()) {
                                m = 0;
                            }
                            BaseDriverVO baseDriverVO = basedrivervlist.get(m++);
                            schedulingDetailShiftsRulesVO.setCarId(baseDriverVO.getCarId());
                            schedulingDetailShiftsRulesVO.setDriverName(baseDriverVO.getDriverName());
                            schedulingDetailShiftsRulesVO.setDriverId(baseDriverVO.getDriverId().toString());
                            schedulingDetailShiftsRulesVO.setCarId(baseDriverVO.getCarId());
                            schedulingDetailShiftsRulesVO.setCarType(baseDriverVO.getCarType());
                            schedulingDetailShiftsRulesVO.setCarNo(baseDriverVO.getCarNumber());
                            dateVO.getShiftsRules().add(schedulingDetailShiftsRulesVO);
                        }*/
                    } else {
                        List<BaseDriverVO> basedrivervlist = schedulingPlanMapper.getCarLinId(carLineId);
                        //新增分割数据(新增的时候用)
                        List<SchedulingDetailShiftsRulesVO> shiftsRulesVOS = schedulingPlanMapper.getAllShiftsRules(carLineId);
                        int m = 0;
                        for (int a = 0; a < shiftsRulesVOS.size(); a++) {
                            SchedulingDetailShiftsRulesVO schedulingDetailShiftsRulesVO1 = shiftsRulesVOS.get(a);
                            Random random = new Random();
                            if (m == basedrivervlist.size()) {
                                m = 0;
                            }
                            BaseDriverVO baseDriverVO = basedrivervlist.get(m++);
                            schedulingDetailShiftsRulesVO1.setCarId(baseDriverVO.getCarId());
                            schedulingDetailShiftsRulesVO1.setDriverName(baseDriverVO.getDriverName());
                            schedulingDetailShiftsRulesVO1.setDriverId(baseDriverVO.getDriverId().toString());
                            schedulingDetailShiftsRulesVO1.setCarId(baseDriverVO.getCarId());
                            schedulingDetailShiftsRulesVO1.setCarType(baseDriverVO.getCarType());
                            schedulingDetailShiftsRulesVO1.setCarNo(baseDriverVO.getCarNumber());
                            dateVO.getShiftsRules().add(schedulingDetailShiftsRulesVO1);
                            //dateVO.getShiftsRules().add(schedulingDetailShiftsRulesVO1);
                        }
                    }
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return datas;
    }
}
